package com.example.sports_meet.volunteer;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.MyDbHelper;
import com.example.sports_meet.R;

import java.util.ArrayList;
import java.util.Objects;

public class VolunteerExitRace extends AppCompatActivity {

    private ListView listView;
    private Button btnDel;
    private String name;

    private MyDbHelper myDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volunteer_exit_race);

        Objects.requireNonNull(getSupportActionBar()).hide();
        myDbHelper = new MyDbHelper(this, "db", null, 2);
        btnDel = findViewById(R.id.btn_exit);

        SharedPreferences sharedPreferences = getSharedPreferences("spfRecord", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "未登录");
        showData(account);
    }

    public void showData(String studentNo) {

        SQLiteDatabase db = myDbHelper.getWritableDatabase();

        // 查询指定学号的记录
        String query = "SELECT vno,vname,eno,ename FROM vapply WHERE vno='" + studentNo + "'";
        Cursor cursor = db.rawQuery(query, null);

        // 将查询结果添加到List中
        ArrayList<String> listData = new ArrayList<>();
        while (cursor.moveToNext()) {
            String data = "学号：" + cursor.getString(0) +
                    "\n姓名：" + cursor.getString(1) +
                    "\n赛事编号：" + cursor.getString(2) +
                    "\n赛事名称：" + cursor.getString(3);
            listData.add(data);
        }

        // 将List数据绑定到ListView中
        ListView listView = findViewById(R.id.tv_selected);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, listData);
        listView.setAdapter(adapter);

        // 设置ListView的Item点击事件，用于删除操作
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                btnDel.setOnClickListener(new View.OnClickListener() {
                    String itemData = parent.getItemAtPosition(position).toString();
                    String[] dataArr = itemData.split("\n");
                    String studentNo = dataArr[0].substring(3);
                    String eventNo = dataArr[2].substring(5);

                    @Override
                    public void onClick(View v) {
                        db.delete("vapply", "vno=? AND eno=?", new String[]{studentNo, eventNo});
                        listView.refreshDrawableState();

                        Toast.makeText(VolunteerExitRace.this, "退赛成功", Toast.LENGTH_SHORT).show();
                        // 更新ListView
                        showData(studentNo);
                    }
                });

            }
        });
    }
}